package com.b2c.ftysys.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.b2c.ftysys.constants.FtySysConstants;
import com.b2c.ftysys.domain.ExampleFactory;
import com.b2c.ftysys.domain.OrderExample;
import com.b2c.ftysys.domain.PageResult;
import com.b2c.ftysys.domain.PdtorderExample;
import com.b2c.ftysys.helper.DateUtils;
import com.b2c.ftysys.sevice.OrderReviewService;
import com.b2c.ftysys.sevice.ServicesException;
import com.b2c.ftysys.vo.OrderItemVo;
import com.b2c.ftysys.vo.OrderVo;

@Controller
public class OrderAction {
	private final static String ADD_ITEMS_FLAG = "add_items";

	@Autowired
	private ServletContext servletContext = null;

	@Autowired
	OrderReviewService orderService = null;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

	@RequestMapping(value = "order")
	public String toOrderPage(HttpServletRequest request) {
		PageResult page = new PageResult();
		page.setCurPage(1);

		OrderExample example = new OrderExample();
		example.createCriteria().andOrderdateGreaterThan(
				DateUtils.getLastMonthTime());
		example.setOrderByClause(" orderDate desc ");
		page.setSqlParamObj(example);

		request.setAttribute("dataList", orderService.getOrderList(page));
		request.setAttribute("vo", new OrderVo());
		return "order/order";
	}

	@RequestMapping(value = "orderReview")
	public String orderReview(HttpServletRequest request) {
		return "order/input";
	}

	@RequestMapping(method = RequestMethod.POST, value = "orderReview")
	public String orderReview(HttpServletRequest request,
			@RequestParam("excelFile") CommonsMultipartFile mFile) {

		if (!mFile.isEmpty()) {
			String path = servletContext.getRealPath("/")
					+ FtySysConstants.IMPORT_EXCEL_FILE_PREFIX; // 获取本地存储路径
			String orderFileName = new Date().getTime()
					+ FtySysConstants.IMPORT_EXCEL_FILE_SUBFIX;
			File file = new File(path + orderFileName); // 新建一个文件
			try {
				mFile.getFileItem().write(file);
				request.setAttribute("orderVo",
						orderService.imortReview(file.getAbsolutePath()));
				request.setAttribute("today", new Date());
				request.setAttribute("filePath", orderFileName);
			} catch (ServicesException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
			request.setAttribute("fileAddr", mFile.getFileItem().getName());
		}
		return "order/input";
	}
	
	@RequestMapping(method = RequestMethod.POST, value="orderQuery")
	public String queryOrder(OrderVo vo, HttpServletRequest request){
		if(null==vo)vo = new OrderVo();
		
		if(!StringUtils.isEmpty(vo.getCmtorderid())){
			OrderExample example = ExampleFactory.createOrderExample();
			example.createCriteria().andCmtorderidEqualTo(vo.getCmtorderid());
			example.setOrderByClause(" orderDate desc ");
			PageResult page = new PageResult();
			page.setSqlParamObj(example);
			request.setAttribute("dataList", orderService.getOrderList(page ));
		}
		
		request.setAttribute("vo", vo);
		return "order/order";
	}

	@RequestMapping(method = RequestMethod.POST, value = "order")
	public String saveOrder(OrderVo vo, HttpServletRequest request) {
		String addItemFlag = request.getParameter("addItemFlag");
		if (ADD_ITEMS_FLAG.equalsIgnoreCase(addItemFlag)) {
			// 采购日期 条码 SKU号 产品属性 数量 发货日期
			String[] cmtPdtIds = request.getParameterValues("cmtPdtId");
			String[] cmtPdtOrderIds = request
					.getParameterValues("cmtPdtOrderId");
			String[] attributes = request.getParameterValues("attribute");
			String[] sendDates = request.getParameterValues("sendDate");
			String[] orderNums = request.getParameterValues("orderNum");

			OrderItemVo item = null;

			for (int i = 0; i < cmtPdtOrderIds.length; i++) {
				item = new OrderItemVo();
				try {
					item.setArrivedate(vo.getOrderdate());
					item.setOrderid(vo.getCmtorderid());
					item.setCmtpdtid(cmtPdtIds[i]);
					item.setCmtpdtorderid(cmtPdtOrderIds[i]);
					item.setAttribute(attributes[i]);
					item.setOrdernum(new Integer(orderNums[i]));
					item.setSenddate(DateUtils.parseToDate(sendDates[i],
							DateUtils.yyyyMMdd));
					item.setTrackNo(i+1);

					vo.addItem(item);
				} catch (Exception e) {
					e.printStackTrace();
					continue;
				}
			}
		}

		if (null != vo.getOrderItems()) {
			vo.setTotalNum(vo.getOrderItems().size());
		} else {
			vo.setTotalNum(0);
		}
		orderService.saveOrder(vo);
		return "redirect:/order.do";
	}

	@RequestMapping("getOrder.do")
	public String toOrderItemsPage(HttpServletRequest request) {
		String orderId = request.getParameter("orderId");
		PageResult page = new PageResult();
		PdtorderExample pdtOrderExample = ExampleFactory
				.createPdtOrderExample();
		pdtOrderExample.setOrderByClause(" trackNo ");
		page.setSqlParamObj(pdtOrderExample);
		page.setPageSize(200);
		request.setAttribute("orderId", orderId);

		request.setAttribute("dataList",
				orderService.getOrderItemList(orderId, page));
		return "order/orderItems";
	}
}
